**********************************************************************
** title:	The rural-urban cleavage in US presidential elections	**
** data:	coded ANES/CES and GSS files							**
** purpose:	replicate all analyses 									**
** authors:	pautonnier et al.										**
**********************************************************************


/*

** TABLE OF CONTENTS

** 1.	Preamble 
** 2. 	Figure 1
** 3. 	Figure 2
** 4. 	Figure 3
** 5. 	Table 1
** 6. 	Figures 4 and 5
** 7. 	Appendix B - models with state FE
** 8. 	Appendix C - swing states
** 9. 	Appendix D - correlation ANES/CES vs. GSS effects
** 10. 	Appendix E - midterm elections
** 11. 	Appendix G - urban vs. suburban areas
*/



* -------------
* 1. preamble
* -------------

	** set working directory 
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication"
	
	** set scheme for graphs
	grstyle init
	grstyle set plain, nogrid noextend	
	
	** install software
	
	/* go to https://www.romain-lachat.ch/software.html, download the cindex.ado (written by R. Lachat) file and save it in your personal Stata folder. */
	
	/* install the clarify package in stata (written by Tomz et al.) */
	*ssc install clarify
	
	** install standard packages (uncomment if not installed yet)
	*ssc install outreg2
	*ssc install coefplot

	

* ----------------------------------
* 2. figure 1 (main manuscript)
* ----------------------------------
	
	** load data
	use "Data/ANESCCES-coded.dta", clear

	** keep only information from presidential election years
	drop if year==1954
	drop if year==1958
	drop if year==1962
	drop if year==1966
	drop if year==1970
	drop if year==1974
	drop if year==1978
	drop if year==1982
	drop if year==1986
	drop if year==1990
	drop if year==1994
	drop if year==1998
	
	** set working directory for saving tables and figures
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication/Tables and Figures"

	** additional data coding
	encode state, gen(statenum)
	tab religion, gen(religiongroup)

	** define election years local
	local years 1952 1960 1964 1968 1972 1976 1980 1984 1988 1992 1996 2000 2008 2012 2016 2020
	local recent_years 2008 2012 2016 2020

	** make sure weight variable is properly set 
	replace weight = 1 if year >= 1952 & year < 2004

	** estimate bivariate regressions for each election year
	tempname model1
	postfile `model1' year b_rural se_rural using model1_results.dta, replace

	foreach y of local years {
    reg voterep rural if year == `y' [pweight=weight]
    margins, dydx(rural)
    post `model1' (`y') (`=_b[rural]') (`=_se[rural]')
	}
	postclose `model1'

	** estimate multivariate regressions (with controls) for each election year
	tempname model2
	postfile `model2' year b_rural se_rural using model2_results.dta, replace

	local years 1952 1960 1964 1968 1972 1976 1980 1984 1988 1992 1996 2000 2008 2012 2016 2020

	foreach y of local years {
    reg voterep rural female i.agegroup i.education white i.religion_recoded ///
         i.income laborunion married migration_background children ///
        i.census if year == `y' [pweight=weight]
    margins, dydx(rural)
    post `model2' (`y') (`=_b[rural]') (`=_se[rural]')
	}
	postclose `model2'


	** visualize the coefficients

	preserve
	use model1_results.dta, clear
	replace year = year - 0.5
	gen model = 1

	append using model2_results.dta
	replace model = 2 if model == .
	replace year = year + 0.5 if model == 2

	gen lower = b_rural - (1.96 * se_rural)
	gen upper = b_rural + (1.96 * se_rural)

	gen dataset_new = (model == 1 & inlist(year, 2007.5, 2011.5, 2015.5, 2019.5)) | ///
                  (model == 2 & inlist(year, 2008.5, 2012.5, 2016.5, 2020.5))

	gen b_m1_old = b_rural if model == 1 & dataset_new == 0
	gen b_m1_new = b_rural if model == 1 & dataset_new == 1
	gen b_m2_old = b_rural if model == 2 & dataset_new == 0  
	gen b_m2_new = b_rural if model == 2 & dataset_new == 1

	gen lower_m1_old = lower if model == 1 & dataset_new == 0
	gen upper_m1_old = upper if model == 1 & dataset_new == 0
	gen lower_m1_new = lower if model == 1 & dataset_new == 1
	gen upper_m1_new = upper if model == 1 & dataset_new == 1

	gen lower_m2_old = lower if model == 2 & dataset_new == 0
	gen upper_m2_old = upper if model == 2 & dataset_new == 0
	gen lower_m2_new = lower if model == 2 & dataset_new == 1
	gen upper_m2_new = upper if model == 2 & dataset_new == 1

	twoway (rspike lower_m1_old upper_m1_old year, lcolor(black)) ///
       (rspike lower_m1_new upper_m1_new year, lcolor(black)) ///
       (rspike lower_m2_old upper_m2_old year, lcolor(black)) ///
       (rspike lower_m2_new upper_m2_new year, lcolor(black)) ///
       (scatter b_m1_old year, msymbol(circle) mcolor(white) msize(medium) mlcolor(black)) ///
       (scatter b_m1_new year, msymbol(square) mcolor(white) msize(medium) mlcolor(black)) ///
       (scatter b_m2_old year, msymbol(circle) mcolor(black) msize(medium)) ///
       (scatter b_m2_new year, msymbol(square) mcolor(black) msize(medium) mlcolor(black) mlwidth(medium)), ///
       yline(0, lpattern(dash)) xlabel(1952(4)2020) ///
       ylabel(-0.3(0.1)0.2) xtitle("") ytitle("AME of rural on Republican vote") ///
       legend(off) ///
       xsize(4.5) ysize(2.5)
       graph export "Figures/ANESCES_AME_RURAL_Republican.png", replace

	restore
	erase "model1_results.dta"
	erase "model2_results.dta"

	clear


* ----------------------------------
* 3. figure 2 (main manuscript)
* ----------------------------------

	** load data
	clear all
	set more off
	set maxvar 10000
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication/Data"
	use "GSS-coded.dta", clear
	
	** set working directory for saving tables and figures
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication/Tables and Figures"

	** define election years local
	local years 1972 1976 1980 1984 1988 1992 1996 2000 2004 2008 2012 2016 2020
	local years_income 1976 1980 1984 1988 1992 1996 2000 2004 2008 2012 2016 2020

	** estimate bivariate regressions for each election year
	tempname model1
	postfile `model1' year b_rural se_rural using results_simple.dta, replace

	foreach y of local years_income {
    reg voterep ruraldich if eyear == `y'
    margins, dydx(ruraldich)
    post `model1' (`y') (`=_b[ruraldich]') (`=_se[ruraldich]')
	}
	postclose `model1'

	** estimate multivariate regressions (with controls) for each election year
	tempname model2
	postfile `model2' year b_rural se_rural using results_controls.dta, replace

	foreach y of local years_income {
    reg voterep ruraldich female age i.education white i.religion_recoded ///
        laborunion i.census i.family_income children_recoded migration_background ///
        if eyear == `y'
    margins, dydx(ruraldich)
    post `model2' (`y') (`=_b[ruraldich]') (`=_se[ruraldich]')
	}
	postclose `model2'

	** visualization of the coefficients

	preserve
	use results_simple.dta, clear
	replace year = year - 0.5
	gen model = 1

	append using results_controls.dta
	replace model = 2 if model == .
	replace year = year + 0.5 if model == 2

	gen lower = b_rural - (1.96 * se_rural)
	gen upper = b_rural + (1.96 * se_rural)

	twoway (rspike lower upper year if model == 1, lcolor(black)) ///
       (scatter b_rural year if model == 1, msymbol(circle) mcolor(white) ///
        msize(medium) mlcolor(black)) ///
       (rspike lower upper year if model == 2, lcolor(black)) ///
       (scatter b_rural year if model == 2, msymbol(circle) mcolor(black) ///
        msize(medium)), ///
       yline(0, lpattern(dash)) xlabel(1976(4)2020) ///
       ylabel(-0.3(0.1)0.3) xtitle("") ytitle("AME of rural on Republican vote") ///
       legend(off) ///
       xsize(4.5) ysize(2.5) ///
       saving("Figures/voting_behavior_combined.gph", replace)
	   graph export "Figures/GSS_plot_combined.pdf", replace
	restore

	erase "results_simple.dta"
	erase "results_controls.dta"

	clear

	
* ----------------------------------
* 4. figure 3 (main manuscript)
* ----------------------------------

	** set working directory 
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication"
	
	** load data
	use "Data/ANESCCES-coded.dta", clear

	** estimate lambda index for different cleavages over time (no controls)

	* rural-urban	
	cindex logit voterep rural if year ==1952, save(rural_1952) 
	cindex logit voterep rural if year ==1956, save(rural_1956) 
	cindex logit voterep rural if year ==1960, save(rural_1960) 
	cindex logit voterep rural if year ==1964, save(rural_1964) 
	cindex logit voterep rural if year ==1968, save(rural_1968) 
	cindex logit voterep rural if year ==1972, save(rural_1972) 
	cindex logit voterep rural if year ==1976, save(rural_1976) 
	cindex logit voterep rural if year ==1980, save(rural_1980)
	cindex logit voterep rural if year ==1984, save(rural_1984)
	cindex logit voterep rural if year ==1988, save(rural_1988) 
	cindex logit voterep rural if year ==1992, save(rural_1992)
	cindex logit voterep rural if year ==1996, save(rural_1996) 
	cindex logit voterep rural if year ==2000, save(rural_2000) 
	cindex logit voterep rural if year ==2008, save(rural_2008) 
	cindex logit voterep rural if year ==2012, save(rural_2012) 
	cindex logit voterep rural if year ==2016, save(rural_2016) 
	cindex logit voterep rural if year ==2020, save(rural_2020) 

	rename rural_1952_absl rural_absl_1952
	rename rural_1956_absl rural_absl_1956
	rename rural_1960_absl rural_absl_1960
	rename rural_1964_absl rural_absl_1964
	rename rural_1968_absl rural_absl_1968
	rename rural_1972_absl rural_absl_1972
	rename rural_1976_absl rural_absl_1976
	rename rural_1980_absl rural_absl_1980
	rename rural_1984_absl rural_absl_1984
	rename rural_1988_absl rural_absl_1988
	rename rural_1992_absl rural_absl_1992
	rename rural_1996_absl rural_absl_1996
	rename rural_2000_absl rural_absl_2000
	rename rural_2008_absl rural_absl_2008
	rename rural_2012_absl rural_absl_2012
	rename rural_2016_absl rural_absl_2016
	rename rural_2020_absl rural_absl_2020	

	* race
	cindex logit voterep white if year ==1952, save(white_1952) 
	cindex logit voterep white if year ==1956, save(white_1956) 
	cindex logit voterep white if year ==1960, save(white_1960) 
	*cindex logit voterep white if year ==1964, save(white_1964) // model cannot be estimated because all non-whites vote DEM
	cindex logit voterep white if year ==1968, save(white_1968) 
	cindex logit voterep white if year ==1972, save(white_1972) 
	cindex logit voterep white if year ==1976, save(white_1976) 
	cindex logit voterep white if year ==1980, save(white_1980)
	cindex logit voterep white if year ==1984, save(white_1984)
	cindex logit voterep white if year ==1988, save(white_1988) 
	cindex logit voterep white if year ==1992, save(white_1992)
	cindex logit voterep white if year ==1996, save(white_1996) 
	cindex logit voterep white if year ==2000, save(white_2000) 
	cindex logit voterep white if year ==2008, save(white_2008) 
	cindex logit voterep white if year ==2012, save(white_2012) 
	cindex logit voterep white if year ==2016, save(white_2016) 
	cindex logit voterep white if year ==2020, save(white_2020) 

	rename white_1952_absl white_absl_1952
	rename white_1956_absl white_absl_1956
	rename white_1960_absl white_absl_1960
	*rename white_1964_absl white_absl_1964
	rename white_1968_absl white_absl_1968
	rename white_1972_absl white_absl_1972
	rename white_1976_absl white_absl_1976
	rename white_1980_absl white_absl_1980
	rename white_1984_absl white_absl_1984
	rename white_1988_absl white_absl_1988
	rename white_1992_absl white_absl_1992
	rename white_1996_absl white_absl_1996
	rename white_2000_absl white_absl_2000
	rename white_2008_absl white_absl_2008
	rename white_2012_absl white_absl_2012
	rename white_2016_absl white_absl_2016
	rename white_2020_absl white_absl_2020
	
	* gender 
	cindex logit voterep female if year ==1952, save(female_1952) 
	cindex logit voterep female if year ==1956, save(female_1956) 
	cindex logit voterep female if year ==1960, save(female_1960) 
	cindex logit voterep female if year ==1964, save(female_1964) 
	cindex logit voterep female if year ==1968, save(female_1968) 
	cindex logit voterep female if year ==1972, save(female_1972) 
	cindex logit voterep female if year ==1976, save(female_1976) 
	cindex logit voterep female if year ==1980, save(female_1980)
	cindex logit voterep female if year ==1984, save(female_1984)
	cindex logit voterep female if year ==1988, save(female_1988) 
	cindex logit voterep female if year ==1992, save(female_1992)
	cindex logit voterep female if year ==1996, save(female_1996) 
	cindex logit voterep female if year ==2000, save(female_2000) 
	cindex logit voterep female if year ==2008, save(female_2008) 
	cindex logit voterep female if year ==2012, save(female_2012) 
	cindex logit voterep female if year ==2016, save(female_2016) 
	cindex logit voterep female if year ==2020, save(female_2020) 

	rename female_1952_absl female_absl_1952
	rename female_1956_absl female_absl_1956
	rename female_1960_absl female_absl_1960
	rename female_1964_absl female_absl_1964
	rename female_1968_absl female_absl_1968
	rename female_1972_absl female_absl_1972
	rename female_1976_absl female_absl_1976
	rename female_1980_absl female_absl_1980
	rename female_1984_absl female_absl_1984
	rename female_1988_absl female_absl_1988
	rename female_1992_absl female_absl_1992
	rename female_1996_absl female_absl_1996
	rename female_2000_absl female_absl_2000
	rename female_2008_absl female_absl_2008
	rename female_2012_absl female_absl_2012
	rename female_2016_absl female_absl_2016
	rename female_2020_absl female_absl_2020

	* religion
	tab religion_recoded, gen(religiongroup)
	cindex logit voterep religiongroup1 religiongroup2  if year ==1952, save(religion_1952) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==1956, save(religion_1956) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==1960, save(religion_1960) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==1964, save(religion_1964) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==1968, save(religion_1968) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==1972, save(religion_1972) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==1976, save(religion_1976) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==1980, save(religion_1980) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==1984, save(religion_1984) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==1988, save(religion_1988) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==1992, save(religion_1992) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==1996, save(religion_1996) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==2000, save(religion_2000) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==2000, save(religion_2004) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==2008, save(religion_2008) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==2012, save(religion_2012) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==2016, save(religion_2016) 
	cindex logit voterep religiongroup1 religiongroup2  if year ==2020, save(religion_2020) 
	
	rename religion_1952_absl religion_absl_1952
	rename religion_1956_absl religion_absl_1956
	rename religion_1960_absl religion_absl_1960
	rename religion_1964_absl religion_absl_1964
	rename religion_1968_absl religion_absl_1968
	rename religion_1972_absl religion_absl_1972
	rename religion_1976_absl religion_absl_1976
	rename religion_1980_absl religion_absl_1980
	rename religion_1984_absl religion_absl_1984
	rename religion_1988_absl religion_absl_1988
	rename religion_1992_absl religion_absl_1992
	rename religion_1996_absl religion_absl_1996
	rename religion_2000_absl religion_absl_2000
	rename religion_2008_absl religion_absl_2008
	rename religion_2012_absl religion_absl_2012
	rename religion_2016_absl religion_absl_2016
	rename religion_2020_absl religion_absl_2020

	* income 
	tab income, gen(incomegroup)
	
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==1952, save(income_1952) 
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==1956, save(income_1956) 
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==1960, save(income_1960) 
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==1964, save(income_1964) 
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==1968, save(income_1968) 
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==1972, save(income_1972) 
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==1976, save(income_1976) 
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==1980, save(income_1980)
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==1984, save(income_1984)
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==1988, save(income_1988) 
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==1992, save(income_1992)
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==1996, save(income_1996) 
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==2000, save(income_2000) 
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==2008, save(income_2008) 
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==2012, save(income_2012) 
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==2016, save(income_2016) 
	cindex logit voterep incomegroup2 incomegroup3 incomegroup4 incomegroup5 if year ==2020, save(income_2020) 

	rename income_1952_absl income_absl_1952
	rename income_1956_absl income_absl_1956
	rename income_1960_absl income_absl_1960
	rename income_1964_absl income_absl_1964
	rename income_1968_absl income_absl_1968
	rename income_1972_absl income_absl_1972
	rename income_1976_absl income_absl_1976
	rename income_1980_absl income_absl_1980
	rename income_1984_absl income_absl_1984
	rename income_1988_absl income_absl_1988
	rename income_1992_absl income_absl_1992
	rename income_1996_absl income_absl_1996
	rename income_2000_absl income_absl_2000
	rename income_2008_absl income_absl_2008
	rename income_2012_absl income_absl_2012
	rename income_2016_absl income_absl_2016
	rename income_2020_absl income_absl_2020
	
	* education
	cindex logit voterep higher_educated if year ==1952, save(higher_educated_1952) 
	cindex logit voterep higher_educated if year ==1956, save(higher_educated_1956) 
	cindex logit voterep higher_educated if year ==1960, save(higher_educated_1960) 
	cindex logit voterep higher_educated if year ==1964, save(higher_educated_1964) 
	cindex logit voterep higher_educated if year ==1968, save(higher_educated_1968) 
	cindex logit voterep higher_educated if year ==1972, save(higher_educated_1972) 
	cindex logit voterep higher_educated if year ==1976, save(higher_educated_1976) 
	cindex logit voterep higher_educated if year ==1980, save(higher_educated_1980)
	cindex logit voterep higher_educated if year ==1984, save(higher_educated_1984)
	cindex logit voterep higher_educated if year ==1988, save(higher_educated_1988) 
	cindex logit voterep higher_educated if year ==1992, save(higher_educated_1992)
	cindex logit voterep higher_educated if year ==1996, save(higher_educated_1996) 
	cindex logit voterep higher_educated if year ==2000, save(higher_educated_2000) 
	cindex logit voterep higher_educated if year ==2008, save(higher_educated_2008) 
	cindex logit voterep higher_educated if year ==2012, save(higher_educated_2012) 
	cindex logit voterep higher_educated if year ==2016, save(higher_educated_2016) 
	cindex logit voterep higher_educated if year ==2020, save(higher_educated_2020) 

	rename higher_educated_1952_absl higher_educated_absl_1952
	rename higher_educated_1956_absl higher_educated_absl_1956
	rename higher_educated_1960_absl higher_educated_absl_1960
	rename higher_educated_1964_absl higher_educated_absl_1964
	rename higher_educated_1968_absl higher_educated_absl_1968
	rename higher_educated_1972_absl higher_educated_absl_1972
	rename higher_educated_1976_absl higher_educated_absl_1976
	rename higher_educated_1980_absl higher_educated_absl_1980
	rename higher_educated_1984_absl higher_educated_absl_1984
	rename higher_educated_1988_absl higher_educated_absl_1988
	rename higher_educated_1992_absl higher_educated_absl_1992
	rename higher_educated_1996_absl higher_educated_absl_1996
	rename higher_educated_2000_absl higher_educated_absl_2000
	rename higher_educated_2008_absl higher_educated_absl_2008
	rename higher_educated_2012_absl higher_educated_absl_2012
	rename higher_educated_2016_absl higher_educated_absl_2016
	rename higher_educated_2020_absl higher_educated_absl_2020
	
	** save dataset with lambda indicators
	save "Data/ANESCCES-lambda.dta", replace

	** set working directory for saving tables and figures
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication/Tables and Figures"
	
	** visualize results
	
	preserve

	keep rural_absl_*  white_absl_* female_absl_* religion_absl_* income_absl_* higher_educated_absl_*
	gen id = _n
	reshape long rural_absl_ white_absl_ female_absl_ religion_absl_ income_absl_ higher_educated_absl_, i(id) j(year)

	collapse (mean) rural_absl_ white_absl_ female_absl_ religion_absl_ income_absl_* higher_educated_absl_, by(year)

	rename  rural_absl_ absolutelambdarural
	rename white_absl_ absolutelambdarace
	rename female_absl_ absolutelambdagender
	rename religion_absl_ absolutelambdareligion
	rename income_absl_ absolutelambdaincome
	rename higher_educated_absl absolutelambdaeducation
	twoway (line absolutelambdarural year, lcolor("31 78 121") lwidth(medium)) ///
       (line absolutelambdarace year, lcolor("166 97 26") lwidth(medium)) ///
       (line absolutelambdagender year, lcolor("128 133 133") lwidth(medium)) ///
       (line absolutelambdareligion year, lcolor("91 155 213") lwidth(medium)) ///
       (line absolutelambdaincome year, lcolor("192 80 77") lwidth(medium)) ///
       (line absolutelambdaeducation year, lcolor("112 173 71") lwidth(medium)), ///
       ytitle("Absolute lambda") ///
       legend(order(1 "Rural" 2 "Race" 3 "Gender" ///
                   4 "Religion" 5 "Income" 6 "Education") ///
              rows(2)) ///
       saving("Figures/lambdatime_Rep.gph", replace)

	restore
	
	
* ----------------------------------
* 4. table 1 (main manuscript)
* ----------------------------------	

	** create variables to capture switching

	* abstainers 
	gen abstentionnist=0
	replace abstentionnist = 1 if inrange(CC20_401, 1, 3) | inrange(CC16_401, 1, 3) | inrange(CC401, 1, 3)

	* other voters 
	gen other=.
	replace other=1 if inrange(CC410a,3,6) | inrange(CC16_410a, 3, 5) | CC16_410a==8 | CC20_410==4

	* democratic switchers
	gen switchdemocrat=""
	replace switchdemocrat="Stable Dem" if votedem==1 & (VCF9027 == 1 | CC16_326 == 1 | presvote16post == 1 | CC317 == 1)
	replace switchdemocrat="Switch to Rep" if votedem==0 & (VCF9027 == 1 | CC16_326 == 1 | presvote16post == 1 | CC317 == 1)
	replace switchdemocrat="Switch to Abstention" if abstentionnist==1 & (VCF9027 == 1 | CC16_326 == 1 | presvote16post == 1 | CC317 == 1)
	replace switchdemocrat="Switch to Other" if other==1 & (VCF9027 == 1 | CC16_326 == 1 | presvote16post == 1 | CC317 == 1)

	* republican switchers
	gen switchrepublican=""
	replace switchrepublican="Stable Rep" if votedem==0 & (VCF9027 == 2 | CC16_326 == 2 | presvote16post == 2 | CC317 == 2)
	replace switchrepublican="Switch to Dem" if votedem==1 & (VCF9027 == 2 | CC16_326 == 2 | presvote16post == 2 | CC317 == 2)
	replace switchrepublican="Switch to Abstention" if abstentionnist==1 & (VCF9027 == 2 | CC16_326 == 2 | presvote16post == 2 | CC317 == 2)
	replace switchrepublican="Switch to Other" if other==1 & (VCF9027 == 2 | CC16_326 == 2 | presvote16post == 2 | CC317 == 2) & other==1

	* abstainers switchers
	gen switchabstentionnist=""
	replace switchabstentionnist="Stable Abstentionnist" if abstentionnist==1 & ///
    (VCF9027==0 | CC16_326 == 4 | presvote16post == 7 | CC317 == 4 | inlist(CC316,1,2) | inlist(CC16_316,1,2))
	replace switchabstentionnist="Switch to Dem" if votedem==1 & ///
    (VCF9027==0 | CC16_326 == 4 | presvote16post == 7 | CC317 == 4 | inlist(CC316,1,2) | inlist(CC16_316,1,2))
	replace switchabstentionnist="Switch to Rep" if votedem==0 & ///
    (VCF9027==0 | CC16_326 == 4 | presvote16post == 7 | CC317 == 4 | inlist(CC316,1,2) | inlist(CC16_316,1,2))
	replace switchabstentionnist="Switch to Other" if other==1 & ///
    (VCF9027==0 | CC16_326 == 4 | presvote16post == 7 | CC317 == 4 | inlist(CC316,1,2) | inlist(CC16_316,1,2))

	* others switchers
	gen switchother=""
	replace switchother="Stable Other" if other==1 & ///
    (VCF9027==3 | CC16_326 == 3 | inrange(presvote16post,3,6) | CC317 == 3)
	replace switchother="Switch to Dem" if votedem==1 & ///
    (VCF9027==3 | CC16_326 == 3 | inrange(presvote16post,3,6) | CC317 == 3)
	replace switchother="Switch to Rep" if votedem==0 & ///
    (VCF9027==3 | CC16_326 == 3 | inrange(presvote16post,3,6) | CC317 == 3)
	replace switchother="Switch to Abstention" if abstentionnist==1 & ///
    (VCF9027==3 | CC16_326 == 3 | inrange(presvote16post,3,6) | CC317 == 3)
	
	** information reported in table 1
	
	* top part
	tab switchdemocrat if year==2012
	tab switchdemocrat if year==2016
	tab switchdemocrat if year==2020

	* bottom part
	tab switchrepublican if year==2012
	tab switchrepublican if year==2016
	tab switchrepublican if year==2020	

	
* ---------------------------------------------------------------------
* 5. figures 4 and 5 (main manuscript) and full results (Appendix H)
* ---------------------------------------------------------------------

	** additional data coding

	* encode switching variables
	encode switchdemocrat, gen(switchdemocrat_encoded)
	encode switchrepublican, gen(switchrepublican_encoded)
	encode switchother, gen(switchother_encoded)
	encode switchabstentionnist, gen(switchabstentionnist_encoded)

	* restrict sample
	drop if year < 2012 
	drop if switchdemocrat=="" & switchrepublican=="" & switchother=="" & switchabstentionnist==""

	* create ordered switching variables
	gen switchdemocrat_ordered = .
	replace switchdemocrat_ordered = 1 if switchdemocrat == "Stable Dem"
	replace switchdemocrat_ordered = 2 if switchdemocrat == "Switch to Rep"
	replace switchdemocrat_ordered = 3 if switchdemocrat == "Switch to Other"
	replace switchdemocrat_ordered = 4 if switchdemocrat == "Switch to Abstention"

	gen switchrepublican_ordered = .
	replace switchrepublican_ordered = 1 if switchrepublican == "Stable Rep"
	replace switchrepublican_ordered = 2 if switchrepublican == "Switch to Dem"
	replace switchrepublican_ordered = 3 if switchrepublican == "Switch to Other"
	replace switchrepublican_ordered = 4 if switchrepublican == "Switch to Abstention"

	gen switchother_ordered = .
	replace switchother_ordered = 1 if switchother == "Stable Other"
	replace switchother_ordered = 2 if switchother == "Switch to Rep"
	replace switchother_ordered = 3 if switchother == "Switch to Dem"
	replace switchother_ordered = 4 if switchother == "Switch to Abstention"

	gen switchabst_ordered = .
	replace switchabst_ordered = 1 if switchabstentionnist == "Stable Abstentionnist"
	replace switchabst_ordered = 2 if switchabstentionnist == "Switch to Dem"
	replace switchabst_ordered = 3 if switchabstentionnist == "Switch to Rep"
	replace switchabst_ordered = 4 if switchabstentionnist == "Switch to Other"

	
	* set working directory for saving tables and figures
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication/Tables and Figures"


	* estimate multinominal logit models among democrats
	local years 2012 2016 2020
	
	foreach i of local years {
    mlogit switchdemocrat_ordered rural female i.agegroup i.education i.religion_recoded white i.income ///
        laborunion i.census married migration_background children if year == `i' [pweight=weight], base(1) 
    eststo mlogit_dem_`i'
    
    forvalues outcome = 1/4 {
        margins, dydx(rural) predict(outcome(`outcome')) saving(outcome`outcome'_`i', replace)
    }
}

	* export coefficient tables (Appendix H)
	foreach i of local years {
    esttab mlogit_dem_`i' using "Tables/dem_coefficients_`i'_cat.tex", ///
        replace label se compress stats(N) title("Democratic Switchers Coefficients - `i'")
}

	* create marginal effects plots by year
	foreach i of local years {
    local title_year = cond(`i'==2012, "2008-2012", cond(`i'==2016, "2012-2016", "2016-2020"))
    
    combomarginsplot outcome1_`i' outcome2_`i' outcome3_`i' outcome4_`i', ///
        recastci(rspike) recast(scatter) ///
        yline(0, lpattern(dash)) ///
        xtitle("") ///
        plotopts(msymbol(circle) mcolor(black) mlcolor(black)) ///
        xlabel(1 "Stable Dem." 2 "Switch to Rep." 3 "Switch Other" 4 "Switch Abst.") ///
        title("`title_year'") ///
        saving(graph`i', replace)
}

	* combine Democrat switching graphs -- figure 4 top panels in manuscript
	graph combine graph2012.gph graph2016.gph graph2020.gph, ///
    rows(1) ycommon xsize(10) ysize(4) title("") imargin(0 7 0 7)

	graph export "Figures/switchdemocrat.pdf", replace
	graph export "Figures/switchdemocrat.png", replace
	foreach i of local years {
    forvalues outcome = 1/4 {
        erase "outcome`outcome'_`i'.dta"
    }
    erase "graph`i'.gph"
}

	* estimate multinominal logit models among republicans
	foreach i of local years {
    mlogit switchrepublican_ordered rural female i.agegroup i.education i.religion_recoded white i.income ///
        laborunion i.census married migration_background children if year == `i' [pweight=weight], base(1)
    eststo mlogit_rep_`i'
    
    forvalues outcome = 1/4 {
        margins, dydx(rural) predict(outcome(`outcome')) saving(outcome`outcome'_`i', replace)
    }
	}

	* export coefficient tables (Appendix H)
	foreach i of local years {
    esttab mlogit_rep_`i' using "Tables/rep_coefficients_`i'_cat.tex", ///
        replace label se compress stats(N) title("Republican Switchers Coefficients - `i'")
}

	* create marginal effects plots by year
	foreach i of local years {
    local title_year = cond(`i'==2012, "2008-2012", cond(`i'==2016, "2012-2016", "2016-2020"))
    
    combomarginsplot outcome1_`i' outcome2_`i' outcome3_`i' outcome4_`i', ///
        recastci(rspike) recast(scatter) ///
        yline(0, lpattern(dash)) ///
        xtitle("") ///
        plotopts(msymbol(circle) mcolor(black) mlcolor(black)) ///
        xlabel(1 "Stable Rep." 2 "Switch to Dem." 3 "Switch Other" 4 "Switch Abst.") ///
        title("`title_year'") ///
        saving(graph`i', replace)
}

	* combine Republican switching graphs -- figure 4 bottom panels in manuscript
	graph combine graph2012.gph graph2016.gph graph2020.gph, ///
    rows(1) ycommon xsize(10) ysize(4) title("") imargin(0 7 0 7)

	graph export "Figures/switchrepublican.pdf", replace
	graph export "Figures/switchrepublican.png", replace
	foreach i of local years {
    forvalues outcome = 1/4 {
        erase "outcome`outcome'_`i'.dta"
    }
    erase "graph`i'.gph"
}

	* estimate multinominal logit models among abstainers
	foreach i of local years {
    mlogit switchabst_ordered rural female i.agegroup i.education i.religion_recoded white i.income ///
        laborunion i.census married migration_background children if year == `i' [pweight=weight], base(1)
    eststo mlogit_abs_`i'
    
    forvalues outcome = 1/4 {
        margins, dydx(rural) predict(outcome(`outcome')) saving(outcome`outcome'_`i', replace)
    }
}

	* export coefficient tables (Appendix H)
	foreach i of local years {
    esttab mlogit_abs_`i' using "Tables/abs_coefficients_`i'_cat.tex", ///
        replace label se compress stats(N) title("Abstentionist Switchers Coefficients - `i'")
}

	* create marginal effects plots
	foreach i of local years {
    local title_year = cond(`i'==2012, "2008-2012", cond(`i'==2016, "2012-2016", "2016-2020"))
    
    combomarginsplot outcome1_`i' outcome2_`i' outcome3_`i' outcome4_`i', ///
        recastci(rspike) recast(scatter) ///
        yline(0, lpattern(dash)) ///
        xtitle("") ///
        plotopts(msymbol(circle) mcolor(black) mlcolor(black)) ///
        xlabel(1 "Stable Abst." 2 "Switch Dem." 3 "Switch Rep" 4 "Switch Other", labsize(small)) ///
        title("`title_year'") ///
        saving(graph`i', replace)
}

	* combine Abstentionist switching graphs
	graph combine graph2012.gph graph2016.gph graph2020.gph, ///
    rows(1) ycommon xsize(10) ysize(4) title("") imargin(0 7 0 7)

	graph export "Figures/switchabstentionnist.pdf", replace
	graph export "Figures/switchabstentionnist.png", replace

	foreach i of local years {
    forvalues outcome = 1/4 {
        erase "outcome`outcome'_`i'.dta"
    }
    erase "graph`i'.gph"
	}
	
	clear



* ---------------------------------------
* 7. Appendix B - models with state FE
* ---------------------------------------

	** load data
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication"
	use "Data/ANESCCES-coded.dta", clear

	** keep only information from presidential election years
	drop if year==1954
	drop if year==1958
	drop if year==1962
	drop if year==1966
	drop if year==1970
	drop if year==1974
	drop if year==1978
	drop if year==1982
	drop if year==1986
	drop if year==1990
	drop if year==1994
	drop if year==1998
	
	** set working directory for saving tables and figures
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication/Tables and Figures"
	
	** encode state variable for inclusion in estimation
	encode state, gen(statenum)
	
	** define locals
	local fe_estimates ""
	local fe_labels ""
	local recent_years 2008 2012 2016 2020

	** estimations
	foreach y of local recent_years {
    reg voterep rural female i.agegroup i.education white married migration_background ///
        children i.religion_recoded i.income laborunion  i.statenum if year == `y' [pweight=weight]
    margins, dydx(rural)
    estimates store fe_`y'
    local fe_estimates `fe_estimates' fe_`y' ||
    local fe_labels `fe_labels' `y'
	}

	** plot results
	coefplot `fe_estimates', vertical bycoefs bylabels(`fe_labels') keep(rural) msymbol(O) ///
    yline(0, lpattern(dash)) xsize(3.5) ysize(3) ///
    ytitle("AME of rural on Republican vote") ylabel(-0.2(0.1)0.2)
    graph export "Figures/ANESCES_AME_RURAL_Republican_FE.png", replace


* ---------------------------------------
* 8. Appendix C - swing states
* ---------------------------------------

	** load data and code code additional variables
	
	* load data
	clear
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication"
	use "Data/ANESCCES-coded.dta", clear

	* weight adjustment
	replace weight=1 if year>=1952 & year<2004 

	* encode state variable
	encode state, gen(statenum)

	* swing state coding
	gen swingstate_recent=0
	replace swingstate_recent=1 if state=="NH" | state=="MN" | state=="GA" | state=="AZ" | state=="WI" | state=="PA" | state=="NC" | state=="NV" | state=="MI" | state=="FL" 
	replace swingstate_recent=. if year<2016

	* create temporary directory for intermediate files
	*cap mkdir "temp_analysis"

	** estimations
	eststo m1: reg voterep i.rural##i.swingstate_recent  i.income female i.agegroup i.education white married migration_background children i.religion_recoded laborunion if year ==2016 [pweight=weight]
	eststo m2: reg voterep i.rural##i.swingstate_recent  i.income female i.agegroup i.education white married migration_background children i.religion_recoded laborunion if year ==2020 [pweight=weight]	
	
	** create table
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication/Tables and Figures/Tables"
	esttab m1 m2 using table_appendixC.tex, b(3) se(3) r2 nogap 
	 
* --------------------------------------------------------
* 9. Appendix D - correlation ANES/CES vs. GSS effects
* --------------------------------------------------------

	** load GSS data and append ANES/CES data
	
	* set working directory 
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication"

	* load GSS data
	use "Data/GSS-coded.dta", clear

	* append ANES/CES data
	append using "Data/ANESCCES-coded.dta"
	
	* additional data coding
	replace weight=1 if year>=1952 & year<2004 & dataset=="ANES"

	* define local for election years
	local anes_years 1952 1960 1964 1968 1972 1976 1980 1984 1988 1992 1996 2000 2008 2012 2016 2020
	local gss_years 1972 1976 1980 1984 1988 1992 1996 2000 2004 2008 2012 2016 2020
	local gss_years_income 1976 1980 1984 1988 1992 1996 2000 2004 2008 2012 2016 2020

	* prepare for estimate storage
	tempname combined_results
	postfile `combined_results' election_year b_rural se_rural str16 source ///
         str16 model_type using combined_rural_effects.dta, replace

	* ANES/CES estimates without controls 
	foreach y of local anes_years {
    reg voterep rural if year == `y' & (dataset=="ANES" | dataset=="CES") ///
        [pweight=weight]
    margins, dydx(rural)
    post `combined_results' (`y') (`=_b[rural]') (`=_se[rural]') ///
         ("ANES/CES") ("No controls")
	}

	* ANES/CES estimates with controls 
	foreach y of local anes_years {
    reg voterep rural female i.agegroup i.education white married ///
        migration_background children i.religion_recoded i.income laborunion ///
        i.census if year == `y' & (dataset=="ANES" | dataset=="CES") ///
        [pweight=weight]
    margins, dydx(rural)
    post `combined_results' (`y') (`=_b[rural]') (`=_se[rural]') ///
         ("ANES/CES") ("With controls")
}

	* GSS estimates without controls 
	foreach y of local gss_years {
    reg voterep ruraldich if eyear == `y' & dataset=="GSS"
    margins, dydx(ruraldich)
    post `combined_results' (`y') (`=_b[ruraldich]') (`=_se[ruraldich]') ///
         ("GSS") ("No controls")
}

	* GSS estimates with controls 
	foreach y of local gss_years_income {
    reg voterep ruraldich female age i.education white i.religion_recoded ///
        laborunion i.census i.family_income children_recoded ///
        migration_background if eyear == `y' & dataset=="GSS"
    margins, dydx(ruraldich)
    post `combined_results' (`y') (`=_b[ruraldich]') (`=_se[ruraldich]') ///
         ("GSS") ("With controls")
}

	postclose `combined_results'
	
	* plot the estimates
	use "combined_rural_effects.dta", clear
	
	rename election_year year
	rename source dataset
	replace dataset = subinstr(dataset, "/", "_", .)

	keep if inlist(year, 1972,1976,1980,1984,1988,1992,1996,2000,2008,2012,2016,2020)
	keep if model_type == "With controls"
	reshape wide b_rural se_rural, i(year model_type) j(dataset) string

	pwcorr b_ruralANES_CES b_ruralGSS, sig

	twoway ///
    (scatter b_ruralGSS b_ruralANES_CES, msymbol(square) mcolor(grey) ///
     mlabel(year) mlabposition(45) mlabsize(tiny)) ///
    (lfit b_ruralGSS b_ruralANES_CES, lcolor(gs6)), ///
    xlabel(-0.1(0.05)0.2) ///
    ylabel(-0.1(0.05)0.2) ///
    xtitle("AME Rural - ANES/CES") ///
    ytitle("AME Rural - GSS") ///
    legend(off) ///
    aspect(1) ///
    saving("Tables and Figures/Figures/dataset_correlation.gph", replace)
	graph export "Tables and Figures/Figures/dataset_correlation.pdf", replace
	erase "combined_rural_effects.dta"

	clear


* ---------------------------------------
* 10. Appendix E - midterm elections
* ---------------------------------------

	** set working directory and load data
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication"
	import delimited "Data/CES_cumulative_after_coding.csv", clear

	** additional data coding
	clonevar education=educ

	tab race, gen(racegroup)
	replace white=1 if race==1
	replace white=0 if race!=1 & race!=.
	
	gen race_4cat = 1 if race == 1  // White
	replace race_4cat = 2 if race == 2  // Black
	replace race_4cat = 3 if race == 3  // Hispanic/Latino  
	replace race_4cat = 4 if inlist(race, 4, 5, 6, 7, 8)  // Others (Asian, Native American, Mixed, Other, Middle Eastern)
	label define race_lbl 1 "White" 2 "Black" 3 "Latino" 4 "Others"
	label values race_4cat race_lbl
	
	gen agegroup=1 if age>=17 & age<=24 
	replace agegroup=2 if age>=25 & age<=34 
	replace agegroup=3 if age>=35 & age<=44 
	replace agegroup=4 if age>=45 & age<=54 
	replace agegroup=5 if age>=55 & age<=64 
	replace agegroup=6 if age>=65 & age<=74 
	replace agegroup=7 if age>=75 & age!=. 	
	
	gen laborunion = 0
	replace laborunion = 1 if union == 1 | union == 2  // Personnel syndiqué

	replace laborunion = 1 if (union_hh == 1 | union_hh == 2) & year != 2008  
	replace laborunion = 0 if (union == 3) & (union_hh == 3 | year == 2008)

	gen married = 0
	replace married = 1 if marstat == 1

	gen children = 0
	replace children = 1 if has_child == 1
	
	gen has_child_2=0 if has_child==2
	replace has_child_2=1 if has_child==1
	
	gen religion_4cat = 1 if religion == 1  	// Protestant
	replace religion_4cat = 2 if religion == 2  // Catholic  
	replace religion_4cat = 3 if religion == 6  // Jewish
	replace religion_4cat = 4 if inlist(religion, 3, 4, 5, 7, 8, 9, 10, 11, 12)  // Others (Mormon, Orthodox, Muslim, Buddhist, Hindu, Atheist, Agnostic, Nothing, Something Else)

	gen religion_3cat = 1 if religion == 1  // Protestant
	replace religion_3cat = 2 if religion == 2  // Catholic  
	replace religion_3cat = 4 if inlist(religion, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)  // Others (Mormon, Jewish, Orthodox, Muslim, Buddhist, Hindu, Atheist, Agnostic, Nothing, Something Else)
	
	gen census=1 if v104=="ME"		// Northeast
	replace census=1 if v104=="MA"
	replace census=1 if v104=="CT"
	replace census=1 if v104=="RI"
	replace census=1 if v104=="NY"
	replace census=1 if v104=="PA"
	replace census=1 if v104=="NJ"
	replace census=2 if v104=="ND"	// Midwest
	replace census=2 if v104=="SD"
	replace census=2 if v104=="NE"
	replace census=2 if v104=="KS"
	replace census=2 if v104=="MN"
	replace census=2 if v104=="IA"
	replace census=2 if v104=="MO"
	replace census=2 if v104=="WI"
	replace census=2 if v104=="IL"
	replace census=2 if v104=="MI"
	replace census=2 if v104=="IN"
	replace census=2 if v104=="OH"
	replace census=3 if v104=="OK" // South
	replace census=3 if v104=="TX"
	replace census=3 if v104=="AR"
	replace census=3 if v104=="LA"
	replace census=3 if v104=="KY"
	replace census=3 if v104=="TN"
	replace census=3 if v104=="MS"
	replace census=3 if v104=="AL"
	replace census=3 if v104=="WV"
	replace census=3 if v104=="VA"
	replace census=3 if v104=="MD"
	replace census=3 if v104=="DE"
	replace census=3 if v104=="NC"
	replace census=3 if v104=="SC"
	replace census=3 if v104=="GA"
	replace census=3 if v104=="FL"
	replace census=4 if v104=="WA"	// West
	replace census=4 if v104=="OR"
	replace census=4 if v104=="CA"
	replace census=4 if v104=="MT"
	replace census=4 if v104=="ID"
	replace census=4 if v104=="WY"
	replace census=4 if v104=="NV"
	replace census=4 if v104=="UT"
	replace census=4 if v104=="CO"
	replace census=4 if v104=="AZ"
	replace census=4 if v104=="NM"
	replace census=4 if v104=="AK"
	replace census=4 if v104=="HI"
	
	gen voterep_rep = 0 if voted_rep_party == 1  // Democratic
	replace voterep_rep = 1 if voted_rep_party == 2  // Republican

	save "Data/CES_Cumulate_after_recoding.dta", replace

	** bivariate regression vote Republican fpr House of Representative
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication/Tables and Figures"

	local years 2008 2010 2012 2014 2016 2018 2020
	tempname model1
	postfile `model1' year b_rural se_rural using cumulative_ces_results.dta, replace

	foreach y of local years {
    reg voterep_rep rural if year == `y' [pweight=weight]
    margins, dydx(rural)
    post `model1' (`y') (`=_b[rural]') (`=_se[rural]')
	}
	postclose `model1'
	
	** multivariate regression vote Republican fpr House of Representative
	local years 2008 2010 2012 2014 2016 2018 2020
	tempname model2
	postfile `model2' year b_rural se_rural using cumulative_ces_results_2.dta, replace

	foreach y of local years {
    reg voterep_rep rural female i.agegroup i.education white i.religion_3cat ///
        married children i.income_tranche laborunion i.census if year == `y' [pweight=weight]
    margins, dydx(rural)
    post `model2' (`y') (`=_b[rural]') (`=_se[rural]')
	}
	postclose `model2'
	
	** plot results (Figure E.1 in Supplementary materials)
	preserve
	use cumulative_ces_results.dta, clear
	replace year = year - 0.5
	gen model = 1

	append using cumulative_ces_results_2.dta
	replace model = 2 if model == .
	replace year = year + 0.5 if model == 2

	gen lower = b_rural - (1.96 * se_rural)
	gen upper = b_rural + (1.96 * se_rural)

	twoway (rspike lower upper year if model == 1 & year >2007, lcolor(black)) ///
       (scatter b_rural year if model == 1, msymbol(circle) mcolor(white) msize(medium) mlcolor(black)) ///
       (rspike lower upper year if model == 2 & year >2007, lcolor(black)) ///
       (scatter b_rural year if model == 2, msymbol(circle) mcolor(black) msize(medium)), ///
       yline(0, lpattern(dash)) ///
       ylabel(-0.3(0.1)0.2) xtitle("") ytitle("AME of rural on Republican Representative vote") ///
	   xscale(range(2008(2)2020)) ///
	   xlabel(2008(2)2020) ///
	   legend(off) ///
       xsize(4.5) ysize(2.5) ///
       saving(Figures/representatives_plot_combined1.gph, replace)
	   
	graph export "Figures/representatives_plot_combined1.png", replace
	erase cumulative_ces_results.dta
	erase cumulative_ces_results_2.dta
	restore
	
	
* -------------------------------------------
* 11. Appendix G - urban vs. suburban areas
* -------------------------------------------

	** load the data
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication"
	use "Data/GSS-coded.dta", clear

	** analyses distinguishing 3 place-based groups
	tempname area3_ame
	postfile `area3_ame' year area_type effect se using area3_ame.dta, replace
	
	local years_income 1976 1980 1984 1988 1992 1996 2000 2004 2008 2012 2016 2020

	foreach y of local years_income {
    quietly count if eyear == `y'
    if r(N) > 0 {
        reg voterep i.area_type_simple female age i.education white ///
            i.religion_recoded i.family_income laborunion i.census ///
            children_recoded migration_background if eyear == `y'
        
        margins, dydx(area_type_simple)
        matrix r_b = r(b)
        matrix r_V = r(V)
        
        local eff_sub = r_b[1,2]
        local se_sub = sqrt(r_V[2,2])
        post `area3_ame' (`y') (2) (`eff_sub') (`se_sub')
        
        local eff_rural = r_b[1,3]
        local se_rural = sqrt(r_V[3,3])
        post `area3_ame' (`y') (3) (`eff_rural') (`se_rural')
        
        post `area3_ame' (`y') (1) (0) (0)
    }
	}
	postclose `area3_ame'

	** visualize the results (Figure G.1 in Supplementary Materials)
	preserve
	use "area3_ame.dta", clear
	
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication/Tables and Figures"

	quietly count
	if r(N) > 0 {
    label define areatypes3_me 1 "Urban (ref)" 2 "Suburban vs. Urban" 3 "Rural vs. Urban"
    label values area_type areatypes3_me
    
    gen lower = effect - 1.96*se
    gen upper = effect + 1.96*se
    
    gen offset = 0
    replace offset = -0.2 if area_type==2
    replace offset = 0.2 if area_type==3
    gen plot_year = year + offset
    
    twoway (rspike lower upper plot_year if area_type==2, lcolor(darkgray)) ///
           (scatter effect plot_year if area_type==2, msymbol(diamond) ///
            mcolor(darkgray) msize(medium)) ///
           (rspike lower upper plot_year if area_type==3, lcolor(lightgray)) ///
           (scatter effect plot_year if area_type==3, msymbol(square) ///
            mcolor(white) mlcolor(darkgray) msize(medium)), ///
           yline(0, lpattern(dash)) ///
           xlabel(1976(4)2020, angle(45)) ///
           ylabel(-0.1(0.1)0.3) ///
           xtitle("Year") ///
           ytitle("Average Marginal Effect on Republican vote") ///
           legend(order(2 "Suburban vs. Urban" 4 "Rural vs. Urban")) ///
           saving("Figures/area3_marginal_effects.gph", replace)
    
    graph export "Figures/area3_marginal_effects.png", replace
	}
	restore
	cd "~/Dropbox/JELS_Rural_Urban_USA/Harvard Dataverse replication"
	erase "area3_ame.dta"













